package com.xmad.ksadutils.AdSDK.AdMold.Kwai;

import android.app.Activity;
import android.util.Log;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.kwad.sdk.api.KsAdSDK;
import com.kwad.sdk.api.KsAdVideoPlayConfig;
import com.kwad.sdk.api.KsFeedAd;
import com.kwad.sdk.api.KsLoadManager;
import com.kwad.sdk.api.KsScene;
import com.xmad.ksadutils.AdSDK.AdBase.AdBaseLoad;
import com.xmad.ksadutils.AdSDK.AdBase.AdCode;
import com.xmad.ksadutils.AdSDK.Interface.AdLoad;

import java.util.List;

public class KsLoadFeedAd extends AdBaseLoad implements AdLoad {
    private volatile static KsLoadFeedAd mInstance;
    KsFeedAd Ad;
    int Position;
    /**
     * 单例模式
     *
     * @return
     */
    public static KsLoadFeedAd getInstance() {
        if (mInstance == null) {
            synchronized (KsLoadFeedAd.class) {
                if (mInstance == null) {
                    mInstance = new KsLoadFeedAd();
                }
            }
        }
        return mInstance;
    }


    @Override
    public void Show(Activity activity, FrameLayout frameLayout) {
      //  if (Ads==null){
            if (KsAdId==null&& AdCode.getKsLoadFeedAdId()==0){
                Log.e(TAG,"KsLoadFeedAd————————还未设置ID:");
                return;
            }
            if (ToastCode){
                Log.e(TAG,KsAdId+"mmmmmmmmmmm");
                Toast.makeText(activity,KsAdId!=null ? KsAdId+"": AdCode.getKsLoadFeedAdId()+"",Toast.LENGTH_SHORT).show();
            }
            KsScene scene = new KsScene.Builder(KsAdId!=null ?  KsAdId:AdCode.getKsLoadFeedAdId()).adNum(1).build();
            KsAdSDK.getLoadManager()
                    .loadFeedAd(scene, new KsLoadManager.FeedAdListener() {
                        @Override
                        public void onError(int code, String msg) {
                            Log.e(TAG,"KsFeedAd广告请求失败"+ code + msg);
                            if (listener!=null){
                                listener.onError(code+msg);
                            }
                        }

                        @Override
                        public void onFeedAdLoad(@Nullable List<KsFeedAd> adList) {
                            if (adList == null || adList.isEmpty()) {
                                Log.e(TAG, "广告数据为空");
                                return;
                            }
                            for (KsFeedAd ksFeedAd : adList) {
                                if (ksFeedAd == null) { continue; }
                                KsAdVideoPlayConfig videoPlayConfig = new KsAdVideoPlayConfig.Builder().videoSoundEnable(true) // 是否有声播放
                                        .dataFlowAutoStart(false) // 是否非WiFi下自动播放
                                        .build();
                                ksFeedAd.setVideoPlayConfig(videoPlayConfig);
                                Ad=adList.get(0);
                               // if (!Cache){
                                    frameLayout.removeAllViews();
                                    FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                                    lp.gravity = Gravity.CENTER;
                                    frameLayout.addView(Ad.getFeedView(activity),lp);
                                    frameLayout.setPadding(10,10,10,10);
                                    ksFeedAd.setAdInteractionListener(new KsFeedAd.AdInteractionListener() {
                                        @Override
                                        public void onAdClicked() {
                                            Log.e(TAG, "广告点击回调");
                                        }

                                        @Override
                                        public void onAdShow() {
                                            Log.e(TAG, "信息流展示");
                                            //setCache(true);
                                            Ad=null;
                                            Cache=true;
                                         //   Show(activity,frameLayout);
                                            if (listener!=null){
                                                listener.onAdShow();
                                            }
                                        }

                                        @Override
                                        public void onDislikeClicked() {
                                            Log.e(TAG, "广告不喜欢回调");
                                            adList.remove(ksFeedAd);
                                        }

//                                        @Override
//                                        public void onDownloadTipsDialogShow(){
//
//                                        }
//
//                                        @Override
//                                        public void onDownloadTipsDialogDismiss(){
//
//                                        }
                                    });
//                                }else {
//                                    Log.e(TAG,"+++++信息流广告预加载+++++++++"+Ads.toString());
//                                }
                            }
                        }
                    });
//        }else {
//            Log.e(TAG,"显示缓存"+Ads.toString());
//            frameLayout.addView(Ads.getFeedView(activity));
//            frameLayout.setPadding(5,5,5,5);
//            Ads.setAdInteractionListener(new KsFeedAd.AdInteractionListener() {
//                @Override
//                public void onAdClicked() {
//                    Log.e(TAG, "广告点击回调");
//                }
//
//                @Override
//                public void onAdShow() {
//                    Log.e(TAG, "显示回调"+Ads.toString()+"成功");
//                    setCache(true);
//                    Ads=null;
//                    Show(activity,frameLayout);
//                }
//
//                @Override
//                public void onDislikeClicked() {
//                    Log.e(TAG, "广告不喜欢回调");
//                }
//            });
//        }

    }




}
